<?php
/**
 * 好友邀请管理
 * 
 * @author fanzhanao<fanzhanao@78.com>
 * @version $Id: invitation.php 10130 2010-08-20 06:59:02Z fanzhanao $
 */
class friends_controller_invitation extends SnsController
{
	/**
	 * friends_model_invitation
	 *
	 * @var object
	 */
	public $_mod = null;
	
	/**
	 * friends_model_data
	 *
	 * @var object
	 */
	public $_data = null;
	
	/**
	 * 最大好友数
	 *
	 * @var int
	 */
	private static $_maxFriendCount = 500;
	
	public function __construct($app, $controller)
	{
		parent::__construct($app, $controller);
		$this->_mod = Model::factory('friends_model_invitation');
		$this->_data = Model::factory('friends_model_data');
		$this->headerView = $this->viewDir."/_header.phtml";
	}
	
	/**
	 * 自己看到的邀请页面
	 * 
	 * @display true
	 */
	public function invite_form_action()
	{
		$this->requireLogin();
		$this->_mod->gencode(self::$login['uid'], self::$login['nickname']);
		$this->appFrameView = $this->viewDir ."/frame.phtml";
		$this->__title = '邀请';
		$uid	=	self::$login['uid'];
		if(!$invite = $this->_mod->select(self::$login['uid'])){
			$code = $this->_mod->gencode($uid, self::$login['nickname']);
		}else{
			$code = $invite['code'];
		}
		$inviteURL = friends_util_invitation::genLink($uid, $code);
		$this->ret = self::success(array('url'=>$inviteURL, 'txt'=>$invite['txt']));
	}
	
	/**
	 * 保存邀请信息
	 *
	 * @display false
	 */
	public function save_desc_action()
	{
		$this->requireLogin();
		$uid = self::$login['uid'];
		$desc = _('desc');
		$this->_mod->setTxt($uid, $desc);
		$this->addMessage('您的信息已保存');
		$this->_renderJson();
	}
	
	/**
	 * 用户看到的邀请页面
	 *
	 * @display true
	 */
	public function inviting_action()
	{
		$this->appFrameView = $this->viewDir ."/frame.phtml";
		$uid = (int) $this->getUid();
		
		if (isset(self::$login['uid']) && (self::$login['uid'] == $uid)) {
			$this->_redirect();
		}
		
		if (!$uid) {
			$this->ret = self::failure(Error::ERROR_DATA, '数据错误');
			return;
		}
		
		$invite = $this->_mod->select($uid);
		if (!$invite) {
			$this->ret = self::failure(Error::ERROR_DATA, '数据错误');
			return;
		}
		
		if (friends_util_invitation::genCode($uid, $invite['code'])!=$this->invite_code) {
			$this->ret = self::failure(Error::ERROR_DATA , '数据错误');
			return;
		}
		
		$this->ret = self::success($invite);
	}
	
	/**
	 * 执行邀请动作
	 *
	 * @display false
	 */
	public function invite_action()
	{
		$this->_renderJson();
		
		$uid = (int) $this->getUid();
		if (!$uid) {
			$this->ret = self::failure(Error::ERROR_DATA, '数据错误');
			return;
		}
		
		$invite = $this->_mod->select($uid);
		
		if (!$invite) {
			$this->ret = self::failure(Error::ERROR_DATA, '数据错误');
			return;
		}
		
		$this->requireLogin(friends_util_invitation::genLink($uid, $invite['code']));
		
		if (friends_util_invitation::genCode($uid, $invite['code'], self::$login['uid']) != $this->invite_code){
			$this->ret = self::failure(Error::ERROR_DATA , '数据错误');
			return;
		}
		
		try {
			$username = $this->_data->bindFriend(self::$login['uid'], $uid, true);
			print_r($username);
//			feed_util_send::save(self::$login['uid'], self::$login['nickname'], 2, $uid, array('uid' => $uid, 'username' => $username));
		} catch (Exception $err) {
			$this->ret = self::failure(friends_util_exception::$errorDescriptions[$err->getMessage()]);
			return ;
		}
		
		
		sms_util_system::send('', self::$login['uid'], 
			eval_template(
				$_SERVER['LOCALE']['apps']['friends'][1]['message']['invite'], 
				array('host_id'=>$invite['id'], 'host_name'=>$invite['username'])), 
			'friends|invite', '', $_SERVER['REQUEST_TIME']);
		
		sms_util_system::send('', $invite['id'], 
			eval_template(
				$_SERVER['LOCALE']['apps']['friends'][1]['message']['invite'], 
				array('host_id'=>self::$login['uid'], 'host_name'=>self::$login['nickname'])), 
			'friends|invite', '', $_SERVER['REQUEST_TIME']);
		
		$this->ret = self::success(array(
			'uid'		=> $uid,
			'username'	=> $username,
			'host_id'	=> self::$login['uid'],
			'host_name'	=> self::$login['nickname'],
			'type'		=> 'bind'
		));
		
		$this->addMessage('你和'.$username.'已成为好友');
	}
}
